<?php

class SettlementAction extends CommonAction{
	public function order(){

		$prds = $_POST["pid"];
		$sizes = $_POST["size"];;
		$qtyArr = $_POST["qty"];

		$member = $_SESSION[self::GLOBAL_SESSION_USER_KEY];
		
		$orderModel = D("Order");
		$order["order_no"] = get_order_no() . "-" . $member["id"];
		$order["member_id"] = $member["id"];
		$order["first_name"] = $member["first_name"];
		$order["last_name"] = $member["last_name"];

		$order["address1"] = $member["address1"];
		$order["address2"] = $member["address2"];

		$order["country"] = $member["country"];
		$order["state"] = $member["state"];
		$order["city"] = $member["city"];
		$order["post_code"] = $member["post_code"];
		$order["phone"] = $member["phone"];
		$order["fax"] = $member["fax"];
		$order["create_time"] = get_client_time();
		$order["order_ip"] = get_client_ip();
		$order["order_status"] = self::ORDER_STATUS_PENDING;
		$order["currency"] = $_COOKIE["currency"];

		$productModel = D("Product");
		$totalPrice = 0;
		for($i=0;$i<count($prds);$i++){
			$p = $productModel->find($prds[$i]);
			$totalPrice += $p["current_price"] * $qtyArr[$i];
			$tempProduct["product_id"] = $p["id"];
			$tempProduct["product_no"] = $p["product_no"];
			$tempProduct["product_name"] = $p["product_name"];
			$tempProduct["original_price"] = $p["original_price"];
			$tempProduct["discount"] = $p["discount"];
			$tempProduct["current_price"] = $p["current_price"];
			$tempProduct["size"] = $sizes[$i];
			$tempProduct["quantity"] = $qtyArr[$i];
			$products[] = $tempProduct;
		}
		//total price
		$order["price"] = $totalPrice;
		$order["Products"] = $products;

		//级联保存
		$list=$orderModel->relation(true)->add($order);
		if($list!==false){
			$this->display();
		}else{
			$this->error('Order error!');
		}

	}
}

?>